package plateforme.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import plateforme.connexion.*;
import plateforme.model.*;

public class DaoAdmin {
	
	static Connection currentCon = null; 
	 static ResultSet rs = null; 
	 public static Personne login(Personne personne){
		 
		//preparing some objects for connection 
		 Statement stmt = null;
		 String login = personne.getLogin();
		 String password =personne.getPassword();
		 String searchQuery = "select * from personne where login='" +login + "' AND password='" + password + "'";
		
		 
		 
		 try { 
			 
			 //connect to DB 
			 currentCon = ConnectionManager.getConnection();
			 stmt=currentCon.createStatement();
			 rs = stmt.executeQuery(searchQuery);
			 boolean more = rs.next(); 
			 // if user does not exist set the isValid variable to false
			 if (!more) 
			 { 
				 System.out.println("Sorry, you are not a registered user! Please sign up first"); 
				 personne.setValid(false); 
				 } 
			 //if user exists set the isValid variable to true 
			 else if (more)
			 {
			 personne.setValid(true);
			 }
		 
	 }
	 
		 catch (Exception ex)
		 { System.out.println("Log In failed: An Exception has occurred! " + ex);
		 }
		 //some exception handling 
		 finally { 
			 if (rs != null) 
			 { try { rs.close();
			 } catch (Exception e) {}
			 rs = null; 
			 }
			 if (stmt != null)
			 { 
				 try 
				 { stmt.close();
				 } catch (Exception e) {} 
				 stmt = null;
				 }
			 if (currentCon != null) 
			 { try 
			 { currentCon.close();
			 } catch (Exception e) { }
			 
		 
		 
	 currentCon = null;
	 }
}

	 
return personne; 
		 
	 }

}
